package com.tianma315.admin.product.rest;


import java.util.Arrays;

import com.tianma315.admin.common.base.BaseController;
import com.tianma315.admin.common.utils.Result;
import com.tianma315.admin.product.vo.RecallVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.product.domain.RecallDO;
import com.tianma315.admin.product.service.RecallService;

/**
 * 
 * <pre>
 * 召回信息
 * </pre>
 * <small> 2019-07-15 17:14:03 | wen</small>
 */
@Controller
@RequestMapping("/product/recall")
public class RecallController extends BaseController{
	@Autowired
	private RecallService recallService;
	
	@GetMapping()
	@RequiresPermissions("product:recall:recall")
	String Recall(){
	    return "product/recall/recall";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("product:recall:recall")
	public Result<Page<RecallVO>> list(Integer pageNumber, Integer pageSize, RecallVO recallDTO){
		// 查询列表数据
        Page<RecallVO> page = recallService.getRecallPage(pageNumber,pageSize,recallDTO);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("product:recall:add")
	String add(){
	    return "product/recall/add";
	}

	@GetMapping("/edit/{recallId}")
	@RequiresPermissions("product:recall:edit")
	String edit(@PathVariable("recallId") Integer recallId,Model model){
		RecallDO recall = recallService.selectById(recallId);
		model.addAttribute("recall", recall);
	    return "product/recall/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("product:recall:add")
	public Result<String> save( RecallDO recall){
		recall.setCreateBy(getUserId());
		if(recallService.addRecall(recall)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("product:recall:edit")
	public Result<String>  update( RecallDO recall){
		recallService.updateById(recall);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("product:recall:remove")
	public Result<String>  remove( Integer recallId){
		if(recallService.deleteRecall(recallId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("product:recall:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] recallIds){
//		recallService.deleteBatchIds(Arrays.asList(recallIds));
		for (Integer recallId:recallIds) {
			recallService.deleteRecall(recallId);
		}
		return Result.ok();
	}
	
}
